<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="app" style="margin-top: 80px;margin-left: 21px;padding-left: 40px;">
    <h1>频道管理</h1>
    <div>
        <div style="float: left;line-height: 40px;">频道名称:</div>
        <el-input v-model="input" placeholder="请输入内容" style="width: 232px; float: left;margin-left: 30px;"></el-input>
        <div style="float: left;line-height: 40px;margin-left: 120px;margin-right: 16px;">状态:</div>
        <template >
            <el-select v-model="value" placeholder="请选择" style="float: left;">
                <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                </el-option>
            </el-select>

        </template>
        <el-row style="float: left;">

            <el-button type="primary" style="margin-left: 40px;" @click="inquire">查询</el-button>

            <el-button type="primary" style="margin-left: 40px;" @click="add">新建</el-button>

        </el-row>

        <template>
            <el-table
                    :data="tableData"
                    style="width: 100%; padding-top: 40px;">
                <el-table-column
                        prop="channelNumber"
                        label="频道编码"
                        width="260">
                </el-table-column>
                <el-table-column
                        prop="channelName"
                        label="频道名称"
                        width="260">
                </el-table-column>
                <el-table-column
                        prop="articleNumber"
                        label="文章数"
                        width="260">
                </el-table-column>
                <el-table-column
                        prop=""
                        label="状态"
                        width="260">
                    <template slot-scope="scope">
                        <span>{{ scope.row.channelStatus == '0' ? '禁用' : '启用' }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="operate"
                        label="操作"
                        width="260">
                    <template slot-scope="scope">
                        <el-button type="text" @click="modify(scope.row.id)">编辑</el-button>
                        <el-button type="text" @click="open(scope.row.id)">删除</el-button>

                    </template>
                </el-table-column>
            </el-table>
        </template>
        <!--分页组件-->
        <div class="pagination-container">
            <el-pagination
                    class="pagiantion"
                    @current-change="handleCurrentChange"
                    :current-page="pagination.currentPage"
                    :page-size="pagination.pageSize"
                    layout="total, prev, pager, next, jumper"
                    :total="pagination.total">
            </el-pagination>
        </div>
    </div>

</div>
</body>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue@2/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="../data/axios-0.18.0.js"></script>
<script>
    new Vue({
        el: '#app',
        data: function () {

            return {
                tableData: [],
                input: '',
                pagination: {
                    //分页相关模型数据
                    currentPage: 1,	//当前页码
                    pageSize:8,	//每页显示的记录数
                    total:0,		//总记录数
                },
                options: [
                    {
                        value: '选项1',
                        label: '启用'
                    },
                    {
                        value: '选项2',
                        label: '禁用'
                    }
                ],
                value: '',
                label:''
            }

        },
        created() {
            this.init()
        },
        methods: {
            open(id) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // alert(id)

                    axios({
                        url: '/channel/' + id,
                        method: 'delete'
                    }).then(resp => {
                        console.log(resp.data)
                        if (resp.data.code == 1) {
                            this.$message({
                                type: 'success',
                                message: '删除成功'
                            });
                            this.init()
                        } else {
                            this.$message.error(resp.data.msg)
                        }
                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            add() {
                location.href = "channel/add.html"
            },
            modify(id) {

                window.localStorage.setItem('editId', id);

                location.href = "channel/edit.html"
            },
            init(name,value) {
                axios({
                    method: "get",
                    url: "/channel/list/" + this.pagination.currentPage + "/" + this.pagination.pageSize,
                    params:{
                        name : name,
                        state : value
                    }
                }).then(res => {
                    this.pagination.total = res.data.data.total;
                    this.pagination.currentPage = res.data.data.current;
                    this.pagination.pagesize = res.data.data.size;
                    this.tableData = res.data.data.records;
                })
            },
            inquire(){//根据条件查询
                var name = this.input;
                var value = this.value;

                if(value!=null && value!=''){
                    value = value == "选项1" ? "1" : "0"
                }
                this.init(name,value)

            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.init();
            }

        }
    })
</script>
</html>